package com.hkyl.hospital.web.controller;

import com.github.pagehelper.PageInfo;
import com.hkyl.hospital.domain.ResultVo;
import com.hkyl.hospital.entity.Operation;
import com.hkyl.hospital.mapper.OperationMapper;
import com.hkyl.hospital.service.OperationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author sjh
 * @date 2023/07/10 19:54:51
 */
@RestController
@RequestMapping("operation")
public class OperationController {
    Logger logger = LoggerFactory.getLogger(OperationController.class);

    @Autowired
    private OperationService operationService;

    @Autowired
    private OperationMapper operationMapper;

    @PostMapping("save")
    public ResultVo save(@RequestBody Operation operation) {
        int rows = operationService.insert(operation);
        return rows > 0 ? ResultVo.success("添加成功") : ResultVo.failure("添加失败");
    }

    @PostMapping("delete")
    public ResultVo delete(@RequestBody Map map) {
        int count = 0;
        int rows = 0;
        Integer operationId = (Integer) map.get("operationId");
        logger.info(String.valueOf(operationId));
        List<Operation> operations = operationMapper.selectByExample(null);
        for (Operation operation : operations) {
            if (operation.getParentId() == operationId) {
                count++;
            }
        }
        logger.info(String.valueOf(count));
        if (count == 0) {
            rows = operationService.delete(Integer.valueOf(operationId));
        }
        return rows > 0 ? ResultVo.success("删除成功") : ResultVo.failure("删除失败");
    }

    @PostMapping("update")
    public ResultVo update(@RequestBody Operation operation) {
        int rows = operationService.update(operation);
        return rows > 0 ? ResultVo.success("修改成功") : ResultVo.failure("修改失败");
    }

    @PostMapping("select")
    public ResultVo select(@RequestBody Map map) {
        Integer pageNumber = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        PageInfo<Operation> operations = new PageInfo<>(operationService.select(pageNumber, pageSize));
        return ResultVo.success(operations);
    }

    @PostMapping("search")
    public ResultVo search(@RequestBody Map map) {
        String operationName = (String) map.get("operationName");
        Integer pageNumber = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        PageInfo<Operation> operations = new PageInfo<>(operationService.search(operationName, pageNumber, pageSize));
        return ResultVo.success(operations);
    }

    @PostMapping("list")
    public ResultVo operationLoad(){
        return ResultVo.success(operationService.listOperation());
    }
}
